package com.ljd.controller.front;


import com.ljd.client.UcenterClient;
import com.ljd.entity.EduComment;
import com.ljd.service.EduCommentService;
import com.ljd.util.JwtUtils;
import com.ljd.util.R;
import com.ljd.util.vo.UcenterMemberVo;
import com.why.handler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author ljd
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/eduservice/comment")
//@CrossOrigin
public class CommentFrontController {

    @Autowired
    EduCommentService commentService;
    @Autowired
    UcenterClient client;

    @GetMapping("getComments/{current}/{pageSize}/{courseId}")
    public R getComments(@PathVariable Long current,@PathVariable Long pageSize,@PathVariable String courseId){
        Map<String,Object> map = commentService.getComments(current,pageSize,courseId);
        return R.ok().data("map",map);
    }

    @PostMapping("saveComment")
    public R saveComment(@RequestBody EduComment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)) {
            throw new GuliException(20001,"请先登录");
        }
        UcenterMemberVo ucenterMemberVo = client.getUcenterMember(memberId);
        comment.setMemberId(memberId);
        comment.setNickname(ucenterMemberVo.getNickname());
        comment.setAvatar(ucenterMemberVo.getAvatar());
        boolean save = commentService.save(comment);
        if(save){
            return R.ok();
        }else{
            return R.error().message("添加评论失败");
        }
    }
}

